#include "StdAfx.h"
#include "Utility.h"
#include "CustomSliderCtrl.h"
#include "../Resource.h"

#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;

IMPLEMENT_DYNAMIC(CCustomSliderCtrl, CSliderCtrl)

CCustomSliderCtrl::CCustomSliderCtrl(void)
{
	m_nState = SLD_NORMAL;

	m_pBrush = NULL;
	m_pPen = NULL;

	m_hBgDC = m_hDrawDC = NULL;
	m_hPParentWnd = m_hParentWnd = NULL;
	CUtility::ImageFromIDResource(IDR_BTN0, _T("PNG"), m_pImg[0]);
	CUtility::ImageFromIDResource(IDR_BTN1, _T("PNG"), m_pImg[1]);
}

CCustomSliderCtrl::~CCustomSliderCtrl(void)
{
	if(m_pBrush!=NULL){ delete m_pBrush;  m_pBrush=NULL; }
	if(m_pPen!=NULL){ delete m_pPen;  m_pPen=NULL; }

	for(int i = 0; i < 2; ++i)
		if ( m_pImg[i] )
			delete m_pImg[i];
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//Common Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomSliderCtrl::Initialize(HWND hPParentWnd, HWND hParentWnd, HDC hBgDC, HDC hDrawDC, int nParentx, int nParenty)
{
	m_hPParentWnd = hPParentWnd;
	m_hParentWnd = hParentWnd;

	m_hBgDC = hBgDC;
	m_hDrawDC = hDrawDC;

	::GetWindowRect(m_hPParentWnd, m_rtPPRect);
	::GetWindowRect(m_hParentWnd, m_rtPRect);

	GetWindowRect(&m_rtRect);
	m_rtRect.OffsetRect(+nParentx, +nParenty);
	m_rtRect.OffsetRect(-m_rtPRect.left, -m_rtPRect.top);
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Drawing
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomSliderCtrl::DrawItem(LPDRAWITEMSTRUCT di)
{
	DrawUI();
}

void CCustomSliderCtrl::DrawUI()
{
	CRect rectThumb;
	this->GetThumbRect(&rectThumb);
	rectThumb.OffsetRect( +m_rtRect.left, +m_rtRect.top);

	BitBlt(m_hDrawDC, m_rtRect.left, m_rtRect.top, m_rtRect.Width(), m_rtRect.Height(), m_hBgDC, m_rtRect.left, m_rtRect.top, SRCCOPY);
	Graphics graphics( m_hDrawDC );

	if(!m_pBrush)  m_pBrush = new SolidBrush(Color(200, 200, 200, 200));
	if(!m_pPen)  m_pPen = new Pen(m_pBrush);

	graphics.DrawLine(m_pPen, (m_rtRect.left + m_rtRect.right - 2) / 2, m_rtRect.top + 8, (m_rtRect.left + m_rtRect.right - 2) / 2, m_rtRect.bottom - 8); 
	graphics.DrawImage(m_pImg[m_nState], rectThumb.left, rectThumb.top,  rectThumb.Width(), rectThumb.Height());

	CUtility::UpdateWindow(m_hPParentWnd, m_hDrawDC, m_rtPPRect);
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Message
/*********************************************************************************************************************/
/*********************************************************************************************************************/
BEGIN_MESSAGE_MAP(CCustomSliderCtrl, CSliderCtrl)
	ON_WM_DRAWITEM()
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

BOOL CCustomSliderCtrl::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}

void CCustomSliderCtrl::PreSubclassWindow()
{
	// TODO: Add your specialized code here and/or call the base class
	ModifyStyle(0, BS_OWNERDRAW);
	CSliderCtrl::PreSubclassWindow();
}
